<?php include "../service/util/authenticateSession.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
	<title>Loại Sản Phẩm</title>
	<?php 
		include("import.php"); 
		include ("messages.properties.php");
	?>
</head>
<body>
<?php
	include "header.php";
    include_once("../service/CategoryService.php");
?>
<!-- Container -->
<div id="container">
	<div class="shell">
		<!-- Main -->
		<div id="main">
			<div class="cl">&nbsp;</div>
			
			<!-- Content -->
			<div id="content">
				<!-- Box -->
				<div class="box">
                	<?php
                    	$message = "";
						$class = "";
						if (isset($_GET['msg'])) $action = $_GET['msg'];
						
						if(isset($action)){
							if($action == "1") {
								$message = $messages["newCategorySuccess"];
								$class = "msg msg-ok";
							}else if($action == "2"){
                                $message = $messages["newCategorySuccessWithouImage"];
								$class = "msg msg-ok"; 
							}else if($action == "3"){
                                $message = $messages["deleteCategoryOk"];
								$class = "msg msg-ok"; 
							}else if($action == "4"){
                                $message = $messages["deleteCategoryFailHasChild"];
								$class = "msg msg-error"; 
							}
                            
						}
						if ($message == "") $class = 'msg msg-hidden';
					?>
                    <!--Message-->
					<div class="<?php echo $class; ?>" id="msg">
                        <p><strong><?php echo $message; ?></strong></p>
                        <a href="#" onclick="hideMessage();" class="close">close</a>
                    </div>
                    <!--Message-->
					<div class="box-head">
						<h2 class="left">Danh Sách Loại Sản Phẩm</h2> 
						
					</div>
					<!-- End Box Head -->	

					<!-- Table -->
					<div class="table">
						<table width="100%" border="0" cellspacing="0" cellpadding="0">
							<tr>
								<th width="13"><input type="checkbox" class="checkbox" /></th>
                                <th>STT</th>
								<th>Tên Loại Sản Phẩm (Tiếng Việt)</th>
                                <th>Tên Loại Sản Phẩm (Tiếng Anh)</th>
                                <th>Thuộc nhóm</th>
								<th width="110" class="ac">Thao Tác</th>
							</tr>
                            <?php
                                $stt = 1;
                                showAllCategories(0, $stt);
                            ?>
						</table>
						
						<?php
                        function showAllCategories($cat_par_id, $counter){
                            $categories = getAllChildrenCategoryOf($cat_par_id);
                            while($category = mysql_fetch_array($categories)){
                            
							echo "<tr>";
                            echo "<td><input type='checkbox' class='checkbox' /></td>";
                            echo "<td>".$counter."</td>";
							echo "<td><h3><a href='category-detail.php?do=view&n=".$category['CAT_ID']."'>".$category['CAT_NAME_VN']."</a></h3></td>";
                            echo "<td><h3><a href='category-detail.php?do=view&n=".$category['CAT_ID']."'>".$category['CAT_NAME_EN']."</a></h3></td>";
                            
                            if($category['CAT_PARENT_ID'] > 0){
                                $parentCate = mysql_fetch_array(getCategory($category['CAT_PARENT_ID']));
                                echo "<td><h3><a href='category-detail.php?do=view&n=".$category['CAT_PARENT_ID']."'>".$parentCate['CAT_NAME_EN']."-".$parentCate['CAT_NAME_VN']."</a></h3></td>";    
                            }else{
                                echo "<td><h3></h3></td>";
                            }
                            
                            
                            //echo "<td><h3></h3></td>";
							echo "<td>";
                            
                            //Delete Button
                            if(!hasChild($category['CAT_ID']) && !hasProduct($category['CAT_ID'])){
                                echo "<a href='../action/DeleteCategoryAction.php?n=".$category['CAT_ID']."' class='ico del'>Delete</a>";                                
                            }else{
                                echo "<a href='#' onclick=\"window.location.href='category.php?msg=4';\" class='ico del'>Delete</a>"; 
                            }
                            
                            //Edit Button
                            echo "<a href='category-detail.php?do=view&n=".$category['CAT_ID']."' class='ico edit'>Edit</a>";
                            echo "<td>";
							echo "</tr>";
                            
                            $counter++;
                            
                            if(hasChild($category['CAT_ID'])){
                                showAllCategories($category['CAT_ID'], $counter);
                                $counter++;
                            }
                            
                            }
                        }
                        ?>
						
					</div>
					<!-- Table -->
					
				</div>
				<!-- End Box -->
				
                <!-- Box -->
                <div class="box">
                	<!-- Box Head -->
                    <div class="box-head">
                    	<h2>Thao tác</h2>
                    </div>
                    <!-- End ofBox Head -->
                    <div class="box-content">
                    	<div class="buttons">
                            <input type="button" class="button" value="Thêm Loại" onClick="window.location='category-detail.php?do=add'"/> &nbsp;
                            <!--<input type="button" class="button" value="Xóa Loại"/>--> 
                        </div>
                    </div>
                </div>
			</div>
			<!-- End Content -->
			

			
			<div class="cl">&nbsp;</div>			
		</div>
		<!-- Main -->
	</div>
</div>
<!-- End Container -->
<?php
	include "footer.php";
	ConnectionManager::getInstance()->closeConnection();
?>	
</body>
</html>